﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Client
{
    public partial class FileTransferCtrl : UserControl
    {
        public p2p.FileAttr fileAttr;
        private p2p.FileTransfer trans;
        private DateTime oldTime;
        private long byteTransPerCompute = 0;
        public bool receive;

        #region Constructors

        public FileTransferCtrl(p2p.FileAttr fileAttr, bool receive)
        {
            InitializeComponent();
            this.fileAttr = fileAttr;
            this.labelFileName.Text = System.IO.Path.GetFileName(fileAttr.FileName);
            this.labelSpeed.Text = "";
            this.btnConfirm.Tag = this;
            this.btnDenyCancel.Tag = this;
            if (receive)
                this.label1.Text = "接收文件：";
            else this.label1.Text = "发送文件：";
            Icon icon = GetSystemIcon.GetIconByFileType(System.IO.Path.GetExtension(fileAttr.FileName), true);
            if (icon != null)
                this.picBoxIcon.Image = icon.ToBitmap();
            this.receive = receive;
        }

        #endregion

        #region Private Methods

        private void trans_ProgressChanged(object sender, p2p.FileTransferProgressChangedEventArgs e)
        {
            DateTime time = DateTime.Now;
            if (time.Second != this.oldTime.Second)
            {
                this.byteTransPerCompute += e.ByteTransferThisTime;
                double speed = (double)byteTransPerCompute / (time - this.oldTime).TotalSeconds;
                this.oldTime = time;
                System.Diagnostics.Debug.Print("{0}\n{1}\n{2}", speed, time.ToString(), oldTime.ToString());
                if (speed < 1024)
                    this.labelSpeed.Text = String.Format("{0:F0}", speed) + " B/s";
                else if (speed < 1024 * 1024)
                {
                    speed /= 1024;
                    if (speed > 10)
                        this.labelSpeed.Text = String.Format("{0:F0}", speed) + " KB/s";
                    else this.labelSpeed.Text = String.Format("{0:F2}", speed) + " KB/s";
                }
                else /*if (speed < 1024 * 1024 * 1024)*/
                {
                    speed /= 1024 * 1024;
                    if (speed > 10)
                        this.labelSpeed.Text = String.Format("{0:F0}", speed) + " MB/s";
                    else this.labelSpeed.Text = String.Format("{0:F2}", speed) + " MB/s";
                }
                this.byteTransPerCompute = 0;
            }
            else
            {
                this.byteTransPerCompute += e.ByteTransferThisTime;
            }
            this.progressBar.Value = (int)((float)e.ByteHasTransfered / e.FileAttr.FileSize * 100);
        }

        private void trans_TransferFinished(object sender, p2p.FileTransferFinishedEventArgs e)
        {
            if (e.Succeeded)
            {
                if (this.receive)
                {
                    System.IO.File.Move(this.fileAttr.FileName + ".tmp", this.fileAttr.FileName);
                    this.btnConfirm.Text = "打开";
                    this.btnConfirm.Visible = true;
                    this.btnDenyCancel.Text = "关闭";
                    this.labelSpeed.Visible = false;
                }
            }
            OnFileTransferFinished(e);
        }

        #endregion

        #region Public Methods

        public int DoReceive()
        {
            this.trans = new p2p.FileTransfer(this.fileAttr);
            this.trans.ProgressChanged += new p2p.FileTransferProgressChangedEventHandler(trans_ProgressChanged);
            this.trans.TransferFinished += new p2p.FileTransferFinishedEventHandler(trans_TransferFinished);
            int port = trans.GetPort();
            this.labelSpeed.Visible = true;
            this.btnConfirm.Visible = false;
            this.btnDenyCancel.Text = "取消";
            return port;
        }

        public void DoSend(System.Net.IPEndPoint remoteEP)
        {
            this.trans = new p2p.FileTransfer(this.fileAttr);
            this.trans.ProgressChanged += new p2p.FileTransferProgressChangedEventHandler(trans_ProgressChanged);
            this.trans.TransferFinished += new p2p.FileTransferFinishedEventHandler(trans_TransferFinished);
            this.trans.DoSend(remoteEP);
            this.labelSpeed.Visible = true;
            this.btnDenyCancel.Text = "取消";
        }

        public void DoCancel()
        {
            if (this.trans != null)
                trans.DoCancel();
        }

        #endregion

        #region Events

        public event p2p.FileTransferFinishedEventHandler FileTransferFinished;
        protected virtual void OnFileTransferFinished(p2p.FileTransferFinishedEventArgs e)
        {
            if (FileTransferFinished != null)
            {
                Control target = FileTransferFinished.Target as Control;
                if (target != null && target.InvokeRequired)
                    target.Invoke(FileTransferFinished, new object[] { this, e });
                else
                    FileTransferFinished(this, e);
            }
        }

        #endregion
    }
}
